package com.green.common.fusioncharts;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 复合图形
 * 绘制柱状图、饼图
 * @author yinyuqiao
 * @time 2013-1-7 上午11:24:26
 */
public class MultiChart {
	private StringBuffer buffer;
	private double maxX;
	private double minX;
	private double maxY;
	private double minY;
	private String caption=null;
	private String shownames=null;
	private String showvalues=null;
	private String decimals=null;
	private String baseFont="arial";
	private String baseFontSize="14px";
								
	private Map<String, Points[]> map; //数据
	private List<ChartXLabel> label; //标签
	private String[] colors; //颜色
	
	private String getLabel() {// chart的横坐标X轴的分隔线
		StringBuffer temp = new StringBuffer();
		temp.append("<categories verticalLineColor=\"888888\" verticalLineThickness=\"1\">");
		for (ChartXLabel obj : label) {
			temp.append("<category label=\"");
			temp.append(obj.getName());
			temp.append("\" />");
		}
		temp.append("</categories>");
		return temp.toString();
	}
	public void setLabel(List<ChartXLabel> label) {
		this.label = label;
	}
	public Map<String, Points[]> getMap() {
		return map;
	}
	public String[] getColors() {
		return colors;
	}
	public void setColors(String[] colors) {
		this.colors = colors;
	}
	public void setMap(Map<String, Points[]> map) {
		this.map = map;
	}
	
	public MultiChart() {
	
	}
	/**
	 * 构造函数--初始化
	 * @param caption
	 * @param shownames
	 * @param showvalues
	 * @param decimals
	 */
	public MultiChart(String caption, String shownames, String showvalues,
			String decimals) {
		this.caption = caption;
		this.shownames = shownames;
		this.showvalues = showvalues;
		this.decimals = decimals;
	}
	/**
	 * 设置X轴Y轴坐标
	 * 初始化绘图面板
	 * @return 
	 */
	@SuppressWarnings("unchecked")
	public void setXY() { // 设置x y 轴的坐标
		minX = minY = 0.0;
		maxX = maxY = 1.0;
		if (map != null) {
			for (Iterator it = (Iterator) map.entrySet().iterator(); it.hasNext();) {
				Map.Entry<String, Points[]> e = (Map.Entry<String, Points[]>) it.next();
				
				Points[] list = (Points[]) e.getValue();
				@SuppressWarnings("unused")
				String name  =  e.getKey();
				
				for (int i = 0; i < list.length; i++) {
					double tempX = list[i].getX();
					double tempY = list[i].getY();
					if (tempX > maxX) {
						maxX = tempX;
					}else if(tempX < minX){
						minX = tempX;
					}
					if (tempY > maxY) {
						maxY = list[i].getY();
					}else if(tempY < minY){
						minY = tempY;
					}
				}
			}
		} else {
			
		}
		maxY = Math.rint(maxY*4/3);
		minY = Math.rint(minY*3);
		@SuppressWarnings("unused")
		double x = maxX - minX ;
		@SuppressWarnings("unused")
		double y = maxY - minY ;
	}
	/**
	 * 拼接字符串，取得绘图数据
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getMultiChartStr(){
		buffer=new StringBuffer();
		//初始化
		this.setXY();
		try{
			buffer.append("<chart plotGradientColor=\"\" caption=\"" //plotGradientColor=""取消渐变颜色
					+caption
					+ "\" shownames=\""
					+ shownames
					+ "\" showvalues=\""	
					+ showvalues
					+ "\" divLineDecimalPrecision=\""
					+ 4   //小数点后位数
					+ "\" baseFont=\""
					+ baseFont
					+ "\" baseFontSize=\""
					+ 14
					+ "\" outCnvBaseFontSize=\""
					+ 12
					+ "\" decimals=\""
					+ decimals +"\">");
		}catch(Exception e){
			e.printStackTrace();
		}
		buffer.append(getLabel()); // chart的横坐标X轴的分隔线
		
		int i = 0;
		for (Iterator it = (Iterator) map.entrySet().iterator(); it.hasNext();) {
			Map.Entry<String, Points[]> e = (Map.Entry<String, Points[]>) it.next();
			Points[] list = (Points[]) e.getValue();
			String name  =  e.getKey();
			buffer.append(getXLine(list,name,colors[i]));
			i++;
		//	buffer.append(getXLine(list , name, true));
		}
		buffer.append("</chart>");
		
		System.out.println(buffer.toString());
		return buffer.toString();
	}
	private String getXLine(Points[] point, String lineNum,String color) { //chart的 所有的用户点连接成的曲线
		StringBuffer temp = new StringBuffer();
		temp.append("<dataSet seriesName=\""
						+ lineNum
						+ "\" drawLine=\"1\" color=\""+color+"\" anchorSides=\"20\" anchorRadius=\"1\" anchorBgColor=\"ffffff\" anchorBorderColor=\""+color+"\">");
		for (int i = 0; point.length > i; i++) {
			if (point[i] != null)
				temp.append("<set value=\"" + point[i].getY() + "\" />");
		}
		temp.append("</dataSet>");
		return temp.toString();
	}
	/**
	 * 柱状图曲线
	 */
	@SuppressWarnings("unused")
	private String getXLine(Points[] point, String lineNum,boolean flag) {// chart的 所有的用户点连接成的曲线
		StringBuffer temp = new StringBuffer();
		temp.append("<dataSet seriesName=\""
						+ lineNum
						+ "\" renderAs=\"Line\">");
		for (int i = 0; point.length > i; i++) {
			if (point[i] != null)
				temp.append("<set value=\"" + point[i].getY() + "\" />");
		}
		temp.append("</dataSet>");
		return temp.toString();
	}
	

}
